//登录 -- 邀请码填写
let app = getApp()
const api = require('../../../server/api.js');
//推荐码
Page({
  data: {
    inputCode: "",
    invitationCode: "",
    inviteCodeDisabled: false
  },

  onLoad: function (options) {
    let that = this;
    if (app.globalData.shareUserId && app.globalData.shareUserId != "") {
      api.getConsumerById(app.globalData.shareUserId).then(res => {
        if (res.inviteCode) {
          that.setData({
            invitationCode: res.inviteCode,
            inviteCodeDisabled: true
          })
        }
      })
    }
  },

  onShow: function () { },
  onInput: function (e) {
    let inputValue = e.detail.value;
    this.setData({
      inputCode: inputValue
    })
  },

  getPhoneTap: function (e) {
    let encryptedData = e.detail.encryptedData;
    let iv = e.detail.iv;
    let verifyCode = this.data.inputCode;
    if (verifyCode != "" && verifyCode.length != 7) {
      app.showToast("邀请码必须是七位数!")
      return;
    }
    //if (verifyCode) {
    this.requestLogin(verifyCode, encryptedData, iv);
    // } else {
    //   wx.showModal({
    //     title: '提示',
    //     content: '请输入邀请码',
    //   })
    // }
  },

  //请求登录(微信授权登录))
  requestLogin: function (verifyCode, encryptedData, iv) {
    let that = this;
    wx.login({
      success: res => {
        let wxCode = res.code;
        wx.showLoading({
          title: '正在登录',
        })
        let userInfo = app.globalData.userInfo;
        //请求账号登陆接口
        let prams = {
          nickname: userInfo.nickName,
          headimgurl: userInfo.avatarUrl,
          country: userInfo.country,
          province: userInfo.province,
          city: userInfo.city,
          code: wxCode,
          sex: userInfo.gender,
          platformId: app.globalData.platformId,
          companyId: app.globalData.companyId,
          invitationCode: verifyCode,
          encryptedData: encryptedData,
          iv: iv
        };

        api.wxLogin(prams).then(res => {
          //登录成功
          console.log(res);
          app.globalData.userBean = res;
          // _user.saveUser(res);
          wx.hideLoading()
          app.backHome();

        }).catch(msg => {
          console.log(msg)
          wx.hideLoading()
          app.showToast(msg)
        })
      }
    })
  },

})